package com.jdk7.mo.writer;

import com.jdk7.mo.entity.Teacher;
import com.jdk7.mo.repository.TeacherRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class TeacherItemWriter implements ItemWriter<Teacher> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TeacherRepository teacherRepository;

    @Autowired
    public void setTeacherRepository(TeacherRepository teacherRepository) {
        this.teacherRepository = teacherRepository;
    }

    public void write(List<? extends Teacher> teachers) throws Exception {
        for (Teacher teacher : teachers) {
            teacherRepository.save(teacher);
            logger.info("Update or Insert successful.");
        }
    }
}
